package communication;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Message implements Serializable {
	
	public static byte[] byteFromObj(Serializable serializable) throws IOException{
		byte[] bs;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutput output = null;
		try {
			output = new ObjectOutputStream(byteArrayOutputStream);   
			output.writeObject(serializable);
			bs = byteArrayOutputStream.toByteArray();
			} finally {
				output.close();
				byteArrayOutputStream.close();
			}
		return bs;
	}
	
	public static Message objFromByte(byte[] bs) throws IOException, ClassNotFoundException{
		Message message;
		ObjectInput input = null;
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bs);
		try {
			input = new ObjectInputStream(byteArrayInputStream);
			message = (Message) input.readObject();
		} finally {
			input.close();
			byteArrayInputStream.close();
		}
		return message;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8054809742223994483L;
	
	private String command;
	private Exception exception;

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

}
